package org.ouwh.oowiki;

import java.security.Principal;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.management.remote.JMXAuthenticator;
import javax.management.remote.JMXPrincipal;
import javax.security.auth.Subject;

public class MyJMXAuthenticator implements JMXAuthenticator {
	private String tokenPostFix;

	MyJMXAuthenticator(String tokenPostFix) {
		this.tokenPostFix = tokenPostFix;
	}

	@Override
	public Subject authenticate(Object credentials) {
		String[] sCredentials = (String[]) credentials;
		if ("oowikiadmin".equals(sCredentials[0]) && ("ouwh_oowiki_202408" + tokenPostFix).equals(sCredentials[1])) {
			Set<Principal> principals = new HashSet<Principal>();
			principals.add(new JMXPrincipal(sCredentials[0]));
			return new Subject(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET);
		}
		throw new SecurityException("Authentication failed! ");
	}

}
